import {typeOf} from "./typeOf";

export const deepcopy = <T>(data: T): T => {
  let val = data as any;
  switch (typeOf(val)) {
    case 'array':
      return (val).map((item: any) => deepcopy(item))
    case 'object':
      return Object.keys(val).reduce((ret: any, key: string) => {
        ret[key] = deepcopy(val[key])
        return ret
      }, {})
    case 'date':
      const newDate = new Date()
      newDate.setTime((val).getTime())
      return newDate as any
    case 'regexp':
      let pattern = val.valueOf();
      let flags = '';
      flags += pattern.global ? 'g' : '';
      flags += pattern.ignoreCase ? 'i' : '';
      flags += pattern.multiline ? 'm' : '';
      return new RegExp(pattern.source, flags) as any;
    default :
      return val
  }
}
